﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Site
// Project Description :    
// ===============================================================================
// Class Name          :    AnnouncementRepository
// Namespace:          :    Gfw.Repositories.Site
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/28 21:50:34
// Update Time         :    2012/6/28 21:50:34
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Site;
using Gfw.Repository;

namespace Gfw.Repositories.Site
{
    public class AnnouncementRepository : Repository<FctAnnouncement>
    {
        #region Const Fields

        private const int ROWNUM = 10;

        #endregion

        /// <summary>
        /// 条件查询公告
        /// </summary>
        /// <param name="isFront">true:前台调用（会员）；false:后台调用（管理员）</param>
        /// <param name="pageIndex">base on 0</param>
        /// <param name="pageSize"></param>
        /// <param name="pageTotal"></param>
        /// <returns></returns>
        public IList<FctAnnouncement> Query(bool isFront, string key, DateTime start, DateTime end, int pageIndex, int pageSize, out int pageTotal)
        {
            key = "%" + key + "%";
            end = end.AddDays(1);

            string addCondition = string.Empty;
            if (isFront) // 前台，会员调用
                addCondition = " and IsShow = 1 ";
            else
                addCondition = "";

            int total = (int)Session.CreateQuery("select count(*) from FctAnnouncement where AnnouncementKeyWord like :Key" +
                                                 " and AnnouncementDate>=:Start and AnnouncementDate<=:End" + addCondition).SetString("Key", key).SetDateTime("Start", start)
            .SetDateTime("End", end).UniqueResult<long>();
            pageTotal = (total + pageSize - 1) / pageSize;

            var query = Session.CreateQuery("from FctAnnouncement where AnnouncementKeyWord like :Key" +
                                            " and AnnouncementDate>=:Start and AnnouncementDate<=:End" + addCondition + " order by AnnouncementDate desc").SetString("Key", key).SetDateTime("Start", start)
            .SetDateTime("End", end).SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize);
            return query.List<FctAnnouncement>();
        }

        /// <summary>
        /// 查询前十条公告
        /// </summary>
        /// <returns></returns>
        public IList<FctAnnouncement> GetLatestAnnouncements()
        {
            var query = Session.CreateQuery("from FctAnnouncement p where p.IsShow = 1 order by p.AnnouncementDate desc").SetFirstResult(0).SetMaxResults(ROWNUM);
            return query.List<FctAnnouncement>();
        }
    }
}
